package entity

import (
	"time"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//HKZXQYDKDR 还款中小企业贷款导入
type HKZXQYDKDR struct {
	ID    int        `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	KHYF  int        `gorm:"column:KHYF"`
	KHMC  string     `gorm:"column:KHMC;size:200"`
	QYGM  string     `gorm:"column:QYGM;size:200"`
	LLFD  float64    `gorm:"column:LLFD"`
	FKRQ  *time.Time `gorm:"column:FKRQ;type:date;"`
	DQR   *time.Time `gorm:"column:DQR;type:date;"`
	FKJE  float64    `gorm:"column:FKJE"`
	FYJE  float64    `gorm:"column:FYJE"`
	YFF   float64    `gorm:"column:YFF"`
	DQYFF float64    `gorm:"column:DQYFF"`
	DRJG  string     `gorm:"column:DRJG;"`
}

//HkzxqydkBeforeImportValidation 导入前执行，验证数据
func HkzxqydkBeforeImportValidation(index int, rowdatas []string) []base.ErrorData {
	sql := "SELECT ID FROM T_COMP_CLIE WHERE CUNM=?"
	rows, _ := base.DB.Raw(sql, rowdatas[0]).Rows()
	var CUID int
	if rows.Next() {
		rows.Scan(&CUID)
	}
	errDatas := make([]base.ErrorData, 1, 1)
	if CUID == 0 {
		errInfos := make([]base.ErrorInfo, 1, 1)
		errinfo := base.ErrorInfo{
			ColIndex: 0,
			Message:  "客户名称错误错误或不存在！",
		}
		errInfos[0] = errinfo
		errData := base.ErrorData{
			RowIndex:   index,
			DataValues: rowdatas,
			ErrorInfos: errInfos,
		}
		errDatas[0] = errData
		return errDatas
	}
	return nil
}

//HkzxqydkAfterImport 导入后执行
func HkzxqydkAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	// err = tx.Exec("UPDATE T_BASE_YXFY_HKZXQYDK SET CUID=B.ID "+
	// 	"FROM T_BASE_YXFY_HKZXQYDK A INNER JOIN T_COMP_CLIE B ON  RTRIM(LTRIM(A.KHMC)) = B.CUNM "+
	// 	"WHERE KHYF=?", params["KHYF"]).Error
	// if err != nil {
	// 	return err
	// }
	prams := make([]interface{}, 1)
	prams[0] = params["KHYF"]
	tx.Exec("EXEC UP_BASE_YXFY_HKZXQYDK_DTIP ?", prams...)
	return nil
}

//TableName 还款中小企业贷款导入表
func (HKZXQYDKDR) TableName() string {
	return "T_BASE_YXFY_HKZXQYDK_DTIP"
}

//HKZXQYDKDREF 返回结构体
func HKZXQYDKDREF() base.EntityFunc {
	return func() interface{} {
		entity := HKZXQYDKDR{}
		return &entity
	}
}

//HKZXQYDKDRESF 返回结构体数组
func HKZXQYDKDRESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []HKZXQYDKDR
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("HKZXQYDKDR", base.NewDefaultDao(HKZXQYDKDREF(), HKZXQYDKDRESF(), nil))
	//base.SetValidationCallBack("HKZXQYDKDR", HkzxqydkBeforeImportValidation)
	base.SetAfterCallBack("HKZXQYDKDR", HkzxqydkAfterImport)
}
